<?php
init();

$path_upload = $folder_img_upload.((!empty($path_upload))? $path_upload : 'files/');

function init()
{
    global $dbsql,$language;
    
    if($_GET['act']=='reset')
    {
        $dbsql->query('drop table tblfiles',true);
    }
    
    $table = "CREATE TABLE `tblfiles` (
              `iId` int(11) NOT NULL AUTO_INCREMENT,
              `cName` varchar(200) DEFAULT NULL,
              `cFile` varchar(250)  DEFAULT NULL,
              `cDescription` text default null,
              `iStatus` int(1) DEFAULT NULL,
              `iPostDate` int(11) DEFAULT NULL,
              `cLanguage` char(10) default null,
              PRIMARY KEY (`iId`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
            
            
    $tablehas = $dbsql->query("show tables like 'tblfiles';");
    if($dbsql->num_rows($tablehas)==0)
    {
        $dbsql->query($table,true);
    }    
}

function ListFiles()
{
    global $dbsql,$language;
    
    $sql_where  = "from tblfiles ";
    
    $sql_select = "select count(iId) as count ".$sql_where;
    $result     = $dbsql->query_fetch($sql_select);
    $count_all  = $result['count'];
	list($lf,$la)=Slit_Page($count_all);
    
    $sql_select = "select * ".$sql_where." order by iId desc limit $lf,$la";
    return $dbsql->load($sql_select);
}

function uploadfile($file_tmp,$folder)
{
    global $func_err,$language;
    
    $name   = $file_tmp['name'];
    $type   = $file_tmp['type'];
    $size   = $file_tmp['size'];
    $temp_name  = $file_tmp['tmp_name'];
    
    if(!is_dir($folder))
    {
        @mkdir($folder);
    }
    //check
    if(!is_dir($folder))
    {
        $func_err   = 'not exists folder: '.$folder;
        return false;   
    }
    if(!empty($name))
    {
        $arrname=explode(".",$imageName);
    	$ename=$arrname[count($arrname)-1];
    	$fname=substr($imageName,0,strlen($imageName)-strlen($ename)-1);
        
        if($ename =='exe' || $ename =='com' || $ename =='php' || $ename =='sh')
        {
            $func_err   = 'file type';
            return false;
        }
        
        if(move_uploaded_file($temp_name,$folder.$name))
        {
            return $name;
        }
     }else
     {
        $func_err   = 'empty file name';
        return false;
     }
}
